﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Streambolics.Data
{
    public class DataTable
    {
        private Dictionary<string, DataTableColumn> _Columns = new Dictionary<string, DataTableColumn> ();
        private string _Name;

        public virtual void MakeStructure (string aName, SqlConnection aConnection)
        {
            _Name = aName;
            GetFromDatabase (aConnection);
        }

        public DataTableColumn this[string aName]
        {
            get
            {
                if (_Columns.ContainsKey (aName))
                {
                    return _Columns[aName];
                }
                else
                {
                    DataTableColumn c = new DataTableColumn (aName);
                    _Columns.Add (aName, c);
                    return c;
                }
            }
        }
        public void GetFromDatabase (SqlConnection aConnection)
        {
            SqlCommand q = new SqlCommand ("", aConnection);
            SqlDataReader dr = q.ExecuteReader ();
            while (dr.Read ())
            {
                this[dr.GetSqlString (0).ToString()].SetCurrentType (dr.GetSqlString (1).ToString());
            }
        }

        public bool BuildAlterModifyStatement (StringBuilder aBuilder)
        {
            bool hasChanges = false;

            aBuilder.Append ("ALTER TABLE ");
            aBuilder.Append (this._Name);
            aBuilder.Append (" MODIFY (");

            foreach (DataTableColumn c in _Columns.Values)
            {
                if (c.BuildAlterStatement (aBuilder, hasChanges))
                {
                    hasChanges = true;
                }
            }

            if (hasChanges)
            {
                aBuilder.Append (")");
            }

            return hasChanges;
        }

        public void CreateColumn (string aName, string aType)
        {
            this[aName].SetExpectedType (aType);
        }

        public void CreateDateColumn (string aName)
        {
            CreateColumn (aName, "datetime");
        }

        public void CreateIdColumn (string aName)
        {
            CreateColumn (aName, "uniqueidentifier");
        }

        public void CreateTextColumn (string aName)
        {
            CreateColumn (aName, "text");
        }

        public void CreateIndex (string aName, string aColumns, bool aUnique)
        {
        }

        public void CreateUniqueId ()
        {
            CreateIdColumn ("id");
            CreateIndex ("id", "primary", true);
        }

        public void CreateAuditColumns ()
        {
            CreateDateColumn ("created");
        }

        public void CreateTrackingColumns ()
        {
            CreateColumn ("generation", "timestamp");
        }

        public void CreateDefinitionColumns ()
        {
            CreateUniqueId ();
            CreateAuditColumns ();
            CreateTextColumn ("name");
        }
    }
}
